<template>
  <div class="student-course-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" class="search-form">
      <el-form-item label="学生姓名">
        <el-input v-model="queryParams.studentName" placeholder="请输入学生姓名" clearable />
      </el-form-item>
      <el-form-item label="学号">
        <el-input v-model="queryParams.studentNo" placeholder="请输入学号" clearable />
      </el-form-item>
      <el-form-item label="课程名称">
        <el-input v-model="queryParams.courseName" placeholder="请输入课程名称" clearable />
      </el-form-item>
      <el-form-item label="教师姓名">
        <el-input v-model="queryParams.teacherName" placeholder="请输入教师姓名" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleAdd">选课</el-button>
        <el-button type="danger" @click="handleBatchDelete">批量删除</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="studentCourseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="学生姓名" prop="studentName" />
      <el-table-column label="学号" prop="studentNo" />
      <el-table-column label="课程名称" prop="courseName" />
      <el-table-column label="教师姓名" prop="teacherName" />
      <el-table-column label="学分" prop="credit" />
      <el-table-column label="成绩" prop="score" />
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)">录入成绩</el-button>
          <el-button type="text" style="color: #F56C6C" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="queryParams.current"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 选课对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生" prop="studentId">
          <el-select v-model="form.studentId" placeholder="请选择学生" filterable>
            <el-option
              v-for="item in studentOptions"
              :key="item.id"
              :label="`${item.name}(${item.studentNo})`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="课程" prop="courseId">
          <el-select v-model="form.courseId" placeholder="请选择课程" filterable>
            <el-option
              v-for="item in courseOptions"
              :key="item.id"
              :label="`${item.name}(${item.teacherName})`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="成绩" prop="score" v-if="form.id">
          <el-input-number v-model="form.score" :min="0" :max="100" :precision="1" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getStudentCourseList, saveStudentCourse, updateStudentCourse, deleteStudentCourse, batchDeleteStudentCourse } from '@/api/studentCourse'
import { getStudentList } from '@/api/student'
import { getCourseList } from '@/api/course'

export default {
  name: 'StudentCourse',
  data() {
    return {
      loading: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        studentName: undefined,
        studentNo: undefined,
        courseName: undefined,
        teacherName: undefined
      },
      // 选课列表
      studentCourseList: [],
      // 总记录数
      total: 0,
      // 选中的数据
      selection: [],
      // 对话框
      dialog: {
        title: '',
        visible: false
      },
      // 表单数据
      form: {
        id: undefined,
        studentId: undefined,
        courseId: undefined,
        score: undefined
      },
      // 表单校验规则
      rules: {
        studentId: [{ required: true, message: '请选择学生', trigger: 'change' }],
        courseId: [{ required: true, message: '请选择课程', trigger: 'change' }],
        score: [{ required: true, message: '请输入成绩', trigger: 'blur' }]
      },
      // 学生选项
      studentOptions: [],
      // 课程选项
      courseOptions: []
    }
  },
  created() {
    this.getList()
    this.getStudentOptions()
    this.getCourseOptions()
  },
  methods: {
    getList() {
      this.loading = true
      getStudentCourseList(this.queryParams).then(res => {
        this.studentCourseList = res.data.records
        this.total = res.data.total
        this.loading = false
      })
    },
    getStudentOptions() {
      getStudentList({ current: 1, size: 1000 }).then(res => {
        this.studentOptions = res.data.records
      })
    },
    getCourseOptions() {
      getCourseList({ current: 1, size: 1000 }).then(res => {
        this.courseOptions = res.data.records
      })
    },
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: 10
      }
      this.getList()
    },
    handleSelectionChange(selection) {
      this.selection = selection
    },
    handleSizeChange(val) {
      this.queryParams.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.current = val
      this.getList()
    },
    handleAdd() {
      this.dialog.title = '选课'
      this.dialog.visible = true
      this.form = {}
    },
    handleUpdate(row) {
      this.dialog.title = '录入成绩'
      this.dialog.visible = true
      this.form = { ...row }
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateStudentCourse(this.form).then(() => {
              this.$message.success('修改成功')
              this.dialog.visible = false
              this.getList()
            })
          } else {
            saveStudentCourse(this.form).then(() => {
              this.$message.success('选课成功')
              this.dialog.visible = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除该选课记录?', '提示', {
        type: 'warning'
      }).then(() => {
        deleteStudentCourse(row.id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    handleBatchDelete() {
      if (!this.selection.length) {
        this.$message.warning('请选择要删除的数据')
        return
      }
      this.$confirm(`确认删除选中的${this.selection.length}条数据?`, '提示', {
        type: 'warning'
      }).then(() => {
        const ids = this.selection.map(item => item.id)
        batchDeleteStudentCourse(ids).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
.student-course-container {
  padding: 20px;
}
.search-form {
  margin-bottom: 20px;
}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 